package com.ymm.platform.core.facade;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymm.platform.api.constants.dto.LltorderDetailsDTO;
import com.ymm.platform.api.constants.dto.OrderShowDTO;
import com.ymm.platform.api.constants.dto.QueryOrderDTO;
import com.ymm.platform.api.facade.PlatformQueryOrderService;
import com.ymm.platform.api.utils.JudgePagination;
import com.ymm.platform.api.utils.Result;
import com.ymm.platform.dao.mapper.QueryOrderMapper;


import com.ymm.system.api.facade.SystemOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 平台查询订单服务
 * @Author: Xuecheng Li
 * @CreateTime: 2022-08-03 20:59
 * @Version: 1.0.0
 */
@Slf4j
@Component
@Service
public class PlatformQueryOrderServiceImpl implements PlatformQueryOrderService {

    @Resource
    private QueryOrderMapper queryOrderMapper;




    private JudgePagination judgePagination=new JudgePagination();




    /**
     * @param pageNum
     * @return List<OrderShowDTO>
     * @Description 平台分页查询所有订单
     * @Param [pageNum]
     */
    @Override
    public Result<PageInfo> queryAllOrder(Integer pageNum,Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.listLltorderDetails();
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }

    /**
     * @param pageNum
     * @param queryOrder
     * @return List<OrderShowDTO>
     * @Description 分页条件查询目标订单
     * @Param [pageNum, queryOrder]
     */
    @Override
    public Result<PageInfo> queryOrderOnCondition(Integer pageNum,Integer pageSize, QueryOrderDTO queryOrder) {
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> lltorderDetailsDTOS = queryOrderMapper.queryOrderOnCondition(queryOrder);
        PageInfo<LltorderDetailsDTO> pageInfo=new PageInfo(lltorderDetailsDTOS);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryOrderInProgress(Integer pageNum,Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.listLltorderDetailsOnP();
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryOrderComplete(Integer pageNum,Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.listLltorderDetailsOnC();
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryOrderCancel(Integer pageNum,Integer pageSize) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.listLltorderDetailsOnCan();
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);




    }

    @Override
    public Result<Boolean> editOrderState(Long orderId, Integer orderCancelType, Integer orderCancelReason, String orderCancelDescription) {
        return null;
    }

    @Override
    public Result<OrderShowDTO> queryOrderDetailsById(Long orderId){
        LltorderDetailsDTO lltorderDetailsDTO =queryOrderMapper.queryOrderDetailsById(orderId);
        OrderShowDTO orderShowDTO = orderDetailsToShow(lltorderDetailsDTO);
        return Result.success(orderShowDTO);
    }



    /**
    * @Author: chencong
    * @Description: 将LltorderDetailsDTO转化为OrderShowDTO
    * @DateTime: 21:10 2022/8/15
    * @Params: [lltorderDetailsDTO]
    * @Return com.ymm.platform.api.constants.dto.OrderShowDTO
    */
    private OrderShowDTO orderDetailsToShow(LltorderDetailsDTO lltorderDetailsDTO){
        OrderShowDTO orderShowDTO = new OrderShowDTO();
        //orderShowDTO.setTradeId(lltorderDetailsDTO.getTradeId());
        orderShowDTO.setOrderId(lltorderDetailsDTO.getOrderId());
        orderShowDTO.setTradeDriverState(lltorderDetailsDTO.getTradeEntity().getTradeDriverState());
        orderShowDTO.setOrderShipperState(lltorderDetailsDTO.getOrderShipperState());
        orderShowDTO.setShipperName(lltorderDetailsDTO.getShipperEntity().getNickname());
        orderShowDTO.setShipperPhone(lltorderDetailsDTO.getShipperEntity().getPhoneNumber());
        orderShowDTO.setDriverName(lltorderDetailsDTO.getDriverEntity().getDriverName());
        orderShowDTO.setDriverPhone(lltorderDetailsDTO.getDriverEntity().getDriverPhone());
        orderShowDTO.setDriverType(lltorderDetailsDTO.getDriverEntity().getDriverType());
        orderShowDTO.setDriverCarNum(lltorderDetailsDTO.getDriverEntity().getDriverCarNum());
        orderShowDTO.setOrderCreateDate(lltorderDetailsDTO.getOrderCreateDate());
        orderShowDTO.setTradeArriveTime(lltorderDetailsDTO.getTradeEntity().getTradeArriveTime());
        orderShowDTO.setOrderPrice(lltorderDetailsDTO.getOrderPrice());
        orderShowDTO.setCargoSrc(lltorderDetailsDTO.getCargoEntity().getCargoSrc());
        orderShowDTO.setCargoDes(lltorderDetailsDTO.getCargoEntity().getCargoDes());
        orderShowDTO.setCargoTip(lltorderDetailsDTO.getCargoEntity().getCargoComment());
        orderShowDTO.setDriverCarType(lltorderDetailsDTO.getDriverEntity().getDriverCarType());

        return orderShowDTO;
    }

    @Override
    public Result<PageInfo> queryOrderListByDriverName(Integer pageNum, Integer pageSize, String driverName) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.queryOrderListByDriverName(driverName);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }

    @Override
    public Result<PageInfo> queryOrderListByShipperName(Integer pageNum, Integer pageSize, String shipperName) {
        judgePagination.judgePaginationPlausibility(pageNum,pageSize);
        List<OrderShowDTO> orderShowDTOS = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<LltorderDetailsDTO> listLltorderDetails = queryOrderMapper.queryOrderListByShipperName(shipperName);
        for(LltorderDetailsDTO lltorderDetailsDTOs:listLltorderDetails){
            orderShowDTOS.add(orderDetailsToShow(lltorderDetailsDTOs));
        }
        PageInfo<OrderShowDTO> pageInfo=new PageInfo(orderShowDTOS);
        return Result.success(pageInfo);
    }



}
